package com.demo.service.impl;

import com.demo.mapper.GroupMapper;
import com.demo.po.GroupPo;
import com.demo.service.IGroupService;
import com.demo.service.common.BizCache;
import com.demo.service.common.BizErrorCode;
import com.demo.service.common.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

@Component("groupService")
public class GroupServiceImpl implements IGroupService {

    private Logger log = LoggerFactory.getLogger(GroupServiceImpl.class);

    @Qualifier("groupMapper")
    @Autowired
    private GroupMapper groupMapper;

    @Override
    public int countGroups(GroupPo cond) {
        log.debug("countGroups starting...");
        int count = 0;
        try {
            count = groupMapper.countGroups(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("countGroups end");
        return count;
    }

    @Override
    public List<GroupPo> findGroupsByPage(GroupPo cond) {
        log.debug("findGroupsByPage starting...");
        List<GroupPo> groups = null;
        try {
            groups = groupMapper.findGroupsByPage(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("findGroupsByPage end");
        return groups;
    }

    @Override
    public List<GroupPo> findAllGroups(GroupPo cond) {
        log.debug("findAllGroups starting...");
        List<GroupPo> groups = null;
        try {
            groups = groupMapper.findAllGroups(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("findAllGroups end");
        return groups;
    }

    @Override
    public GroupPo getGroup(Integer groupid) {
        log.debug("getGroup starting...");
        GroupPo group = null;
        try {
            group = groupMapper.getGroup(groupid);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("getGroup end");
        return group;
    }

    @Override
    public Integer addGroup(GroupPo obj) {
        log.debug("addGroup starting...");
        Integer retval = 0;
        try {
            obj.setAddtime(BizCache.getInstance().getNow());
            retval = groupMapper.addGroup(obj);

            if (retval == 0) {
                throw new BizException(BizErrorCode.EX_SQL_FAIL);
            }
            retval = obj.getGroupid();
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("addGroup end");
        return retval;
    }

    @Override
    public int updateGroup(GroupPo obj) {
        log.debug("updateGroup starting...");
        int retval = 0;
        try {
            obj.setUpdatetime(BizCache.getInstance().getNow());
            retval = groupMapper.updateGroup(obj);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        if (retval == 0) {
            throw new BizException(BizErrorCode.EX_SQL_FAIL);
        }
        log.debug("updateGroup end");
        return retval;
    }

    @Override
    public int deleteGroup(Integer groupid) {
        log.debug("deleteGroup starting...");
        int retval = 0;
        try {
            retval = groupMapper.deleteGroup(groupid);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("deleteGroup end");
        return retval;
    }
}